package co.com.inventarios.persistencia;

import java.util.List;
import java.util.logging.Level;
import javax.persistence.EntityManager;
import javax.persistence.Query;
import co.com.inventarios.modelo.Mngtdc;
import co.com.inventarios.modelo.RequiDoc;
import co.com.inventarios.modelo.Unimed;
import co.com.inventarios.modelo.Vinculado;

public class QueryMngtdcDAO {
	
	
	private EntityManager getEntityManager() {
		return EntityManagerHelper.getEntityManager();
	}
	
	public List<Mngtdc> Buscar_Document_Aut(String Buscar,String clase) {
		
		 String [] Bod = null;
		 String filtro = "";
		 Bod = Buscar.split(",");
		    for (int i = 0 ; i < Bod.length ; i++) {
		    	
		        if(i == 0){
			          filtro =	filtro + " AND ( model.id.tdcclase ='"+clase+"' AND model.id.tdctipo='"+Bod [i]+"' ";	
		        }else{
		        	  filtro =	filtro + " OR model.id.tdcclase ='"+clase+"' AND  model.id.tdctipo='"+Bod [i]+"' ";	  	
		        }
		        
		        if(i == (Bod.length - 1)){
		           filtro =	filtro +")";
		        }
		    }
	    
		
 		try {
 			getEntityManager().clear();
			final String queryString = "select model from Mngtdc model where (model.tdcactivo = 1 ) "                    
			        + filtro
			        +" order by model.id.tdctipo DESC";
			Query query = getEntityManager().createQuery(queryString);
			return query.getResultList();
		} catch (RuntimeException re) {
			EntityManagerHelper.log("find by property name failed",
					Level.SEVERE, re);
			throw re;
		}
	}
	
	

	public Double MngDocnum_Id(String clase , String tipo) {
		EntityManagerHelper.log("finding Mngtdc instance with id: " + clase,
				Level.INFO, null);
		try {
			getEntityManager().clear();
			String queryString = "select model.tdcnumero from  Mngtdc model where model.id.tdcclase='"+clase
			        +"' AND model.id.tdctipo='"+tipo+"' ";
			Query query = getEntityManager().createQuery(queryString);
			List list = query.getResultList();
			//System.out.print("===========================>>>>"+(Double)list.get(0)+"<<<<===========================");
			double incremento = 0;
			
			if(list.size() > 0){
				incremento = (Double)list.get(0);
				incremento++;		
			}

			return incremento;
		} catch (RuntimeException re) {
			EntityManagerHelper.log("find by property name failed",
					Level.SEVERE, re);
			throw re;
		}
	}
	
	
	
	public List<Mngtdc> findByPropertyAll(String propertyName, Object value ,String expresion ) {

		EntityManagerHelper.log("finding Mngtdc instance with property: "
				+ propertyName + ", value: " + value, Level.INFO, null);
		try {
			getEntityManager().clear();
			final String queryString = "select model from Mngtdc model where model."
					+ propertyName + " LIKE '%" + value + "%'  "+expresion+" " ;
			Query query = getEntityManager().createQuery(queryString);

			return query.getResultList();
		} catch (RuntimeException re) {
			EntityManagerHelper.log("find by property name failed",
					Level.SEVERE, re);
			throw re;
		}
	}

	public List<Mngtdc> findBy_all(String propertyName) {

		
		try {
			getEntityManager().clear();
			final String queryString = "select model from Mngtdc model "+propertyName+" ";
			Query query = getEntityManager().createQuery(queryString);
			return query.getResultList();
		} catch (RuntimeException re) {
			EntityManagerHelper.log("find by property name failed",
					Level.SEVERE, re);
			throw re;
		}
	}
	
	
	public Mngtdc findByPropertyActivo(String propertyName, Object value) {
		// TODO Auto-generated method stub
		EntityManagerHelper.log("finding Mngtdc instance with property: "
				+ propertyName + ", value: " + value, Level.INFO, null);
		try {
			getEntityManager().clear();
			final String queryString = "select model from Mngtdc model where model.tdcactivo = 1 AND model."
					+ propertyName + "= :propertyValue";
			Query query = getEntityManager().createQuery(queryString);
			query.setParameter("propertyValue", value);
	
			Mngtdc result = new Mngtdc(); 
			if (query.getResultList().size() > 0){
				result = (Mngtdc)query.getSingleResult();
			}else{
				result = null;
			}
				
			return result;
		} catch (RuntimeException re) {
			EntityManagerHelper.log("find by property name failed",
					Level.SEVERE, re);
			throw re;
		}
	}
	
	public List<Mngtdc> findByPropertyActivoLista(String propertyName) {
		try {
			getEntityManager().clear();
			final String queryString = "select model from Mngtdc model where model.tdcactivo = 1 "+propertyName+" ";
			Query query = getEntityManager().createQuery(queryString);
			return query.getResultList();
		} catch (RuntimeException re) {
			EntityManagerHelper.log("find by property name failed",
					Level.SEVERE, re);
			throw re;
		}
	}

}
